package com.itheima.controller;

import com.itheima.pojo.SysDictType;
import com.itheima.service.SysDictTypeService;
import com.itheima.utils.MessageConstant;
import com.itheima.utils.ResultUtil;
import com.itheima.vo.SysDictTypeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/sysDictType")
@CrossOrigin
public class SysDictTypeController {

    @Autowired
    private SysDictTypeService sysDictTypeService;

    /**
     * 添加字典类型
     *@param sysDictTypeVO
     * @return com.itheima.entity.Result
     *@author itheima
     */
    @PostMapping("/add")
    public String add(@RequestBody SysDictTypeVO sysDictTypeVO) {
        try {
            sysDictTypeService.add(sysDictTypeVO);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(HttpServletResponse.SC_OK, MessageConstant.DICTTYPE_ADD_FAIL);
        }
        return ResultUtil.result(HttpServletResponse.SC_OK, MessageConstant.DICTTYPE_ADD_SUCCESS);
    }

    /**
     * 批量删除字典类型
     *@param sysDictTypeVOList
     * @return com.itheima.entity.Result
     *@author itheima
     */
    @PostMapping("/deleteBatch")
    public String deleteBatch(@RequestBody List<SysDictTypeVO> sysDictTypeVOList) {
        try {
            sysDictTypeService.deleteBatch(sysDictTypeVOList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(HttpServletResponse.SC_UNAUTHORIZED,MessageConstant.DICTTYPE_DELETE_FAIL);
        }
        return ResultUtil.result(HttpServletResponse.SC_OK,MessageConstant.DICTTYPE_DELETE_SUCCESS);
    }

    /**
     * 编辑字典类型
     *@param sysDictTypeVO
     * @return com.itheima.entity.Result
     *@author itheima
     */
    @PostMapping("/edit")
    public String edit(@RequestBody SysDictTypeVO sysDictTypeVO) {
        try {
            sysDictTypeService.edit(sysDictTypeVO);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(HttpServletResponse.SC_OK,MessageConstant.DICTTYPE_EDIT_FAIL);
        }
        return ResultUtil.result(HttpServletResponse.SC_OK,MessageConstant.DICTTYPE_EDIT_SUCCESS);
    }

    /**
     * 获取字典类型列表
     *@param sysDictTypeVO
     * @return com.itheima.entity.Result
     *@author itheima
     */
    @PostMapping("/getList")
    public String getList(@RequestBody SysDictTypeVO sysDictTypeVO) {
        List<SysDictType> sysDictTypeList = sysDictTypeService.getList(sysDictTypeVO);
        return ResultUtil.result(HttpServletResponse.SC_OK,sysDictTypeList);
    }

}
